import java.util.HashMap;

/**
 * @author 龙小虬
 * @date 2021/4/13 22:47
 * -Xmx3M -Xms3M
 */
public class HashMapMemoryLeak {
    public static void main(String[] args) {

        HashMap<HashKey, Integer> map = new HashMap<HashKey, Integer>(1000);
        int counter = 0;
        while (true) {
            //循环插入新对象 new出很多很多内存地址不等的对象但是
            HashKey p = new HashKey("lxq", "lxq666");
            map.put(p, 1);
            counter++;
            if (counter % 1000 == 0) {
                System.out.println("map size: " + map.size());
                System.out.println("运行" + counter
                        + "次后，可用内存剩余" + Runtime.getRuntime().freeMemory() / (1024 * 1024) + "MB");
            }
        }
    }
}

class HashKey {
    private final String id;
    private String name;

    public HashKey(String name, String id) {
        this.name = name;
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

//    @Override
//    public int hashCode() {
//        return id.hashCode();
//    }
//
//    @Override
//    public boolean equals(Object obj) {
//        if (obj instanceof HashKey)
//            return name.equals(((HashKey) obj).name) && id.equals(((HashKey) obj).id);
//        else
//            return false;
//    }
}
